package org.example.netty.firstexample;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

import java.net.URI;


@Slf4j
public class TestServer {
    public static void main(String[] args) {
        new ServerBootstrap()
                //boss和worker
                //boss只负责ServerSocketChannel上accept事件，worker只负责socketChannel上的读写
                .group(new NioEventLoopGroup(),new NioEventLoopGroup(2))
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {

                        nioSocketChannel.pipeline().addLast(new HttpServerCodec());
                        nioSocketChannel.pipeline().addLast(new SimpleChannelInboundHandler<HttpObject>() {
                            @Override
                            protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject httpObject) throws Exception {
                                if (httpObject instanceof HttpRequest) {
                                    HttpRequest httpRequest = (HttpRequest)httpObject;
                                    System.out.println("请求方法名："+httpRequest.method().name());
                                    URI uri = new URI(httpRequest.uri());
                                    if("/favicon.ico".equals(uri.getPath())){
                                        System.out.println("请求"+uri.getPath());
                                        return;
                                    }

                                    ByteBuf content = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
                                    DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);
                                    response.headers().set("Content-Type", "text/plain");
                                    response.headers().set("Content-Length",content.readableBytes());

                                    channelHandlerContext.writeAndFlush(response);

                                    //关闭通道
                                    channelHandlerContext.channel().close();
                                }
                            }

                            @Override
                            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                System.out.println("channel Active");
                                super.channelActive(ctx);
                            }

                            @Override
                            public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
                                System.out.println("channel Registered");
                                super.channelRegistered(ctx);
                            }

                            @Override
                            public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
                                System.out.println("handler Added");
                                super.handlerAdded(ctx);
                            }

                            @Override
                            public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
                                System.out.println("channel Unregistered");
                                super.channelUnregistered(ctx);
                            }

                            @Override
                            public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                                System.out.println("channel Inactive");
                                super.channelInactive(ctx);
                            }
                        });
                    }
                })
                .bind(8888);


    }
}
